{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import DataSetQuery\n",
    "import AsyncDataSetQuery\n",
    "import datetime\n",
    "import json\n",
    "from pandas.io.json import json_normalize\n",
    "import pandas as pd\n",
    "import datetime\n",
    "from datetime import timedelta\n",
    "from dateutil.relativedelta import *\n",
    "\n",
    "import MalardHelpers as m\n",
    "\n",
    "environmentName = 'DEVv2'\n",
    "#Sets the URL of the Malard ServiceGateway.\n",
    "query = DataSetQuery.DataSetQuery('http://localhost:9000',environmentName)\n",
    "\n",
    "asyncquery = AsyncDataSetQuery.AsyncDataSetQuery( 'ws://localhost:9000',environmentName)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[{\"name\":\"cryotempo\"},{\"name\":\"jatest\"},{\"name\":\"mtngla\"},{\"name\":\"mtnglauat\"},{\"name\":\"test\"}]'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query.getParentDataSets()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[{\"name\":\"AIS_BaselineC_FilteredPC\",\"region\":\"antarctic\"},{\"name\":\"oib\",\"region\":\"antarctic\"},{\"name\":\"oib\",\"region\":\"greenland\"},{\"name\":\"GRIS_BaseC_Q2\",\"region\":\"greenland\"}]'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query.getDataSets('cryotempo')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "parentDs = 'cryotempo'\n",
    "dataSet = 'AIS_BaselineC_FilteredPC'\n",
    "region = 'antarctic'\n",
    "\n",
    "bbox = json.loads(query.getDataSetBoundingBox( parentDs,dataSet,region ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'gridCellMinX': -2700000, 'gridCellMaxX': 2700000, 'gridCellMinY': -2200000, 'gridCellMaxY': 2200000, 'minTime': 1291164342, 'maxTime': 1483226555, 'totalPoints': 9540724423, 'numberOfShards': 62630}\n"
     ]
    }
   ],
   "source": [
    "print(bbox)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "minX = bbox['gridCellMinX']\n",
    "maxX = bbox['gridCellMaxX']\n",
    "minY = bbox['gridCellMinY']\n",
    "maxY = bbox['gridCellMaxY']\n",
    "minT = datetime.datetime.fromtimestamp(bbox['minTime'])\n",
    "maxT = datetime.datetime.fromtimestamp(bbox['maxTime'])\n",
    "\n",
    "gridCellsDf = json_normalize(json.loads(query.getGridCells( parentDs, dataSet, region, minX, maxX, minY, maxY, minT, maxT )))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 62630 entries, 0 to 62629\n",
      "Data columns (total 8 columns):\n",
      "maxT              62630 non-null int64\n",
      "maxX              62630 non-null float64\n",
      "maxY              62630 non-null float64\n",
      "minT              62630 non-null int64\n",
      "minX              62630 non-null float64\n",
      "minY              62630 non-null float64\n",
      "numberOfPoints    62630 non-null int64\n",
      "shardName         62630 non-null object\n",
      "dtypes: float64(4), int64(3), object(1)\n",
      "memory usage: 3.8+ MB\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "shardsDf = json_normalize(json.loads(query.getShards( parentDs, dataSet, region, minX, maxX, minY, maxY, minT, maxT )))\n",
    "\n",
    "print(shardsDf.info())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "def checkGridCellByMonth( gc ):\n",
    "    count = 0 \n",
    "    gcStartTime = datetime.datetime.fromtimestamp(gc['minTime'])\n",
    "    gcEndTime = datetime.datetime.fromtimestamp(gc['maxTime'])\n",
    "    minX = int(gc['gridCellMinX'])\n",
    "    maxX = int(gc['gridCellMaxX'])\n",
    "    minY = int(gc['gridCellMinY'])\n",
    "    maxY = int(gc['gridCellMaxY'])\n",
    "    \n",
    "    adjStartTime = datetime.datetime( gcStartTime.year, gcStartTime.month, 1, 0, 0,0  )  \n",
    "    incStartTime = adjStartTime +relativedelta(months=+1)-timedelta(seconds=1)\n",
    "    \n",
    "    while adjStartTime < gcEndTime:\n",
    "        minT = adjStartTime\n",
    "        maxT = incStartTime\n",
    "        \n",
    "        cacheResult = asyncquery.executeQuery( parentDs, dataSet, region, minX, maxX, minY, maxY, minT, maxT,[],[] )\n",
    "        \n",
    "        if cacheResult.status != 'Error: Empty resultset.':\n",
    "            df = m.getDataFrameFromNetCDF(cacheResult.resultFileName)\n",
    "            count = count + len(df['x'])\n",
    "            asyncquery.releaseCache(cacheResult.resultFileName)\n",
    "        \n",
    "        adjStartTime = incStartTime\n",
    "        incStartTime = incStartTime +relativedelta(months=+1)-timedelta(seconds=1)\n",
    "    \n",
    "    return count\n",
    "\n",
    "def checkGridCell( gc ):\n",
    "    count = 0 \n",
    "    gcStartTime = datetime.datetime.fromtimestamp(gc['minTime'])\n",
    "    gcEndTime = datetime.datetime.fromtimestamp(gc['maxTime'])\n",
    "    minX = int(gc['gridCellMinX'])\n",
    "    maxX = int(gc['gridCellMaxX'])\n",
    "    minY = int(gc['gridCellMinY'])\n",
    "    maxY = int(gc['gridCellMaxY'])\n",
    "    minT = gcStartTime\n",
    "    maxT = gcEndTime\n",
    "        \n",
    "    cacheResult = asyncquery.executeQuery( parentDs, dataSet, region, minX, maxX, minY, maxY, minT, maxT,['x'],[] )\n",
    "        \n",
    "    if cacheResult.status != 'Error: Empty resultset.':\n",
    "        df = m.getDataFrameFromNetCDF(cacheResult.resultFileName)\n",
    "        count = count + len(df['x'])\n",
    "        asyncquery.releaseCache(cacheResult.resultFileName)\n",
    "    else:\n",
    "        print(\"Unexpected error... \")\n",
    "        \n",
    "    return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 CumCount=7 MinX=1700000 MinY=1100000 QueryCount=7 GridCellCount=7\n",
      "1 CumCount=156 MinX=1300000 MinY=1400000 QueryCount=149 GridCellCount=149\n",
      "2 CumCount=17665 MinX=1900000 MinY=-300000 QueryCount=17509 GridCellCount=17509\n",
      "3 CumCount=54559 MinX=1900000 MinY=-400000 QueryCount=36894 GridCellCount=36894\n",
      "4 CumCount=102854 MinX=1800000 MinY=-500000 QueryCount=48295 GridCellCount=48295\n",
      "5 CumCount=126399 MinX=1800000 MinY=-700000 QueryCount=23545 GridCellCount=23545\n",
      "6 CumCount=138917 MinX=1700000 MinY=1000000 QueryCount=12518 GridCellCount=12518\n",
      "7 CumCount=147679 MinX=1700000 MinY=0 QueryCount=8762 GridCellCount=8762\n",
      "8 CumCount=165501 MinX=1700000 MinY=-100000 QueryCount=17822 GridCellCount=17822\n",
      "9 CumCount=196190 MinX=1700000 MinY=-400000 QueryCount=30689 GridCellCount=30689\n",
      "10 CumCount=221906 MinX=1700000 MinY=-700000 QueryCount=25716 GridCellCount=25716\n",
      "11 CumCount=235104 MinX=1700000 MinY=-900000 QueryCount=13198 GridCellCount=13198\n",
      "12 CumCount=260311 MinX=1600000 MinY=1200000 QueryCount=25207 GridCellCount=25207\n",
      "13 CumCount=313639 MinX=1600000 MinY=100000 QueryCount=53328 GridCellCount=53328\n",
      "14 CumCount=319090 MinX=1600000 MinY=0 QueryCount=5451 GridCellCount=5451\n",
      "15 CumCount=337502 MinX=1600000 MinY=-100000 QueryCount=18412 GridCellCount=18412\n",
      "16 CumCount=349494 MinX=1600000 MinY=-300000 QueryCount=11992 GridCellCount=11992\n",
      "17 CumCount=391196 MinX=1600000 MinY=-400000 QueryCount=41702 GridCellCount=41702\n",
      "18 CumCount=401717 MinX=1600000 MinY=-500000 QueryCount=10521 GridCellCount=10521\n",
      "19 CumCount=439160 MinX=1600000 MinY=-1200000 QueryCount=37443 GridCellCount=37443\n",
      "20 CumCount=465091 MinX=1500000 MinY=1200000 QueryCount=25931 GridCellCount=25931\n",
      "21 CumCount=518307 MinX=1500000 MinY=100000 QueryCount=53216 GridCellCount=53216\n",
      "22 CumCount=538259 MinX=1500000 MinY=-1100000 QueryCount=19952 GridCellCount=19952\n",
      "23 CumCount=545992 MinX=1500000 MinY=-1300000 QueryCount=7733 GridCellCount=7733\n",
      "24 CumCount=579995 MinX=1400000 MinY=1200000 QueryCount=34003 GridCellCount=34003\n",
      "25 CumCount=683430 MinX=1400000 MinY=1000000 QueryCount=103435 GridCellCount=103435\n",
      "26 CumCount=688164 MinX=1400000 MinY=900000 QueryCount=4734 GridCellCount=4734\n",
      "27 CumCount=717751 MinX=1400000 MinY=-200000 QueryCount=29587 GridCellCount=29587\n",
      "28 CumCount=717822 MinX=1200000 MinY=-1700000 QueryCount=71 GridCellCount=71\n",
      "29 CumCount=749414 MinX=1400000 MinY=-500000 QueryCount=31592 GridCellCount=31592\n",
      "30 CumCount=762131 MinX=1400000 MinY=-600000 QueryCount=12717 GridCellCount=12717\n",
      "31 CumCount=774531 MinX=1400000 MinY=-700000 QueryCount=12400 GridCellCount=12400\n",
      "32 CumCount=798596 MinX=1400000 MinY=-1000000 QueryCount=24065 GridCellCount=24065\n",
      "33 CumCount=827356 MinX=1300000 MinY=1200000 QueryCount=28760 GridCellCount=28760\n",
      "34 CumCount=924135 MinX=1300000 MinY=1000000 QueryCount=96779 GridCellCount=96779\n",
      "35 CumCount=1008137 MinX=1300000 MinY=900000 QueryCount=84002 GridCellCount=84002\n",
      "36 CumCount=1054649 MinX=1300000 MinY=800000 QueryCount=46512 GridCellCount=46512\n",
      "37 CumCount=1085865 MinX=1300000 MinY=-100000 QueryCount=31216 GridCellCount=31216\n",
      "38 CumCount=1104839 MinX=1300000 MinY=-200000 QueryCount=18974 GridCellCount=18974\n",
      "39 CumCount=1150869 MinX=1300000 MinY=-400000 QueryCount=46030 GridCellCount=46030\n",
      "40 CumCount=1165058 MinX=1300000 MinY=-800000 QueryCount=14189 GridCellCount=14189\n",
      "41 CumCount=1170181 MinX=1300000 MinY=-1000000 QueryCount=5123 GridCellCount=5123\n",
      "42 CumCount=1179459 MinX=1300000 MinY=-1200000 QueryCount=9278 GridCellCount=9278\n",
      "43 CumCount=1210174 MinX=1300000 MinY=-1400000 QueryCount=30715 GridCellCount=30715\n",
      "44 CumCount=1267590 MinX=1300000 MinY=-1600000 QueryCount=57416 GridCellCount=57416\n",
      "45 CumCount=1271116 MinX=1200000 MinY=1300000 QueryCount=3526 GridCellCount=3526\n",
      "46 CumCount=1275928 MinX=1200000 MinY=1000000 QueryCount=4812 GridCellCount=4812\n",
      "47 CumCount=1337259 MinX=1200000 MinY=900000 QueryCount=61331 GridCellCount=61331\n",
      "48 CumCount=1407438 MinX=1200000 MinY=800000 QueryCount=70179 GridCellCount=70179\n",
      "49 CumCount=1448898 MinX=1200000 MinY=500000 QueryCount=41460 GridCellCount=41460\n",
      "50 CumCount=1514120 MinX=1200000 MinY=400000 QueryCount=65222 GridCellCount=65222\n",
      "51 CumCount=1562334 MinX=1200000 MinY=200000 QueryCount=48214 GridCellCount=48214\n",
      "52 CumCount=1641182 MinX=1200000 MinY=100000 QueryCount=78848 GridCellCount=78848\n",
      "53 CumCount=1683571 MinX=1200000 MinY=-100000 QueryCount=42389 GridCellCount=42389\n",
      "54 CumCount=1693205 MinX=1200000 MinY=-200000 QueryCount=9634 GridCellCount=9634\n",
      "55 CumCount=1696369 MinX=1500000 MinY=-700000 QueryCount=3164 GridCellCount=3164\n",
      "56 CumCount=1705915 MinX=1200000 MinY=-500000 QueryCount=9546 GridCellCount=9546\n",
      "57 CumCount=1730821 MinX=1200000 MinY=-1100000 QueryCount=24906 GridCellCount=24906\n",
      "58 CumCount=1746412 MinX=1200000 MinY=-1200000 QueryCount=15591 GridCellCount=15591\n",
      "59 CumCount=1801946 MinX=1200000 MinY=-1500000 QueryCount=55534 GridCellCount=55534\n",
      "60 CumCount=1818975 MinX=1100000 MinY=1000000 QueryCount=17029 GridCellCount=17029\n",
      "61 CumCount=1886425 MinX=1100000 MinY=800000 QueryCount=67450 GridCellCount=67450\n",
      "62 CumCount=1893878 MinX=1100000 MinY=600000 QueryCount=7453 GridCellCount=7453\n",
      "63 CumCount=2002091 MinX=1100000 MinY=400000 QueryCount=108213 GridCellCount=108213\n",
      "64 CumCount=2004368 MinX=1100000 MinY=300000 QueryCount=2277 GridCellCount=2277\n",
      "65 CumCount=2029716 MinX=1100000 MinY=200000 QueryCount=25348 GridCellCount=25348\n",
      "66 CumCount=2097833 MinX=1100000 MinY=-400000 QueryCount=68117 GridCellCount=68117\n",
      "67 CumCount=2147076 MinX=1100000 MinY=-700000 QueryCount=49243 GridCellCount=49243\n",
      "68 CumCount=2149284 MinX=1100000 MinY=-800000 QueryCount=2208 GridCellCount=2208\n",
      "69 CumCount=2160290 MinX=1100000 MinY=-900000 QueryCount=11006 GridCellCount=11006\n",
      "70 CumCount=2211469 MinX=1100000 MinY=-1000000 QueryCount=51179 GridCellCount=51179\n",
      "71 CumCount=2213787 MinX=1100000 MinY=-1300000 QueryCount=2318 GridCellCount=2318\n",
      "72 CumCount=2232935 MinX=1900000 MinY=-500000 QueryCount=19148 GridCellCount=19148\n",
      "73 CumCount=2319642 MinX=1000000 MinY=1400000 QueryCount=86707 GridCellCount=86707\n",
      "74 CumCount=2331963 MinX=1000000 MinY=1300000 QueryCount=12321 GridCellCount=12321\n",
      "75 CumCount=2359019 MinX=1000000 MinY=1200000 QueryCount=27056 GridCellCount=27056\n",
      "76 CumCount=2384159 MinX=1000000 MinY=1000000 QueryCount=25140 GridCellCount=25140\n",
      "77 CumCount=2392790 MinX=1000000 MinY=900000 QueryCount=8631 GridCellCount=8631\n",
      "78 CumCount=2448131 MinX=1000000 MinY=700000 QueryCount=55341 GridCellCount=55341\n",
      "79 CumCount=2473497 MinX=1000000 MinY=300000 QueryCount=25366 GridCellCount=25366\n",
      "80 CumCount=2581042 MinX=1000000 MinY=100000 QueryCount=107545 GridCellCount=107545\n",
      "81 CumCount=2586221 MinX=1000000 MinY=0 QueryCount=5179 GridCellCount=5179\n",
      "82 CumCount=2617870 MinX=1000000 MinY=-300000 QueryCount=31649 GridCellCount=31649\n",
      "83 CumCount=2617874 MinX=1000000 MinY=-500000 QueryCount=4 GridCellCount=4\n",
      "84 CumCount=2626935 MinX=1000000 MinY=-700000 QueryCount=9061 GridCellCount=9061\n",
      "85 CumCount=2674138 MinX=1000000 MinY=-900000 QueryCount=47203 GridCellCount=47203\n",
      "86 CumCount=2680072 MinX=1000000 MinY=-1100000 QueryCount=5934 GridCellCount=5934\n",
      "87 CumCount=2680126 MinX=1000000 MinY=-1200000 QueryCount=54 GridCellCount=54\n",
      "88 CumCount=2697153 MinX=1000000 MinY=-1400000 QueryCount=17027 GridCellCount=17027\n",
      "89 CumCount=2701945 MinX=1000000 MinY=-1500000 QueryCount=4792 GridCellCount=4792\n",
      "90 CumCount=2718726 MinX=1000000 MinY=-1600000 QueryCount=16781 GridCellCount=16781\n",
      "91 CumCount=2746842 MinX=1000000 MinY=-1700000 QueryCount=28116 GridCellCount=28116\n",
      "92 CumCount=2754334 MinX=900000 MinY=1200000 QueryCount=7492 GridCellCount=7492\n",
      "93 CumCount=2764245 MinX=900000 MinY=1100000 QueryCount=9911 GridCellCount=9911\n",
      "94 CumCount=2811774 MinX=900000 MinY=1000000 QueryCount=47529 GridCellCount=47529\n",
      "95 CumCount=2875488 MinX=900000 MinY=600000 QueryCount=63714 GridCellCount=63714\n",
      "96 CumCount=2895300 MinX=900000 MinY=500000 QueryCount=19812 GridCellCount=19812\n",
      "97 CumCount=2913859 MinX=900000 MinY=400000 QueryCount=18559 GridCellCount=18559\n",
      "98 CumCount=2937462 MinX=900000 MinY=200000 QueryCount=23603 GridCellCount=23603\n",
      "99 CumCount=2984126 MinX=900000 MinY=100000 QueryCount=46664 GridCellCount=46664\n",
      "100 CumCount=3029876 MinX=900000 MinY=0 QueryCount=45750 GridCellCount=45750\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "101 CumCount=3054910 MinX=900000 MinY=-100000 QueryCount=25034 GridCellCount=25034\n",
      "102 CumCount=3094418 MinX=900000 MinY=-400000 QueryCount=39508 GridCellCount=39508\n",
      "103 CumCount=3110273 MinX=900000 MinY=-500000 QueryCount=15855 GridCellCount=15855\n",
      "104 CumCount=3151026 MinX=900000 MinY=-600000 QueryCount=40753 GridCellCount=40753\n",
      "105 CumCount=3230172 MinX=900000 MinY=-900000 QueryCount=79146 GridCellCount=79146\n",
      "106 CumCount=3231902 MinX=900000 MinY=-1000000 QueryCount=1730 GridCellCount=1730\n",
      "107 CumCount=3260726 MinX=900000 MinY=-1200000 QueryCount=28824 GridCellCount=28824\n",
      "108 CumCount=3386833 MinX=800000 MinY=1500000 QueryCount=126107 GridCellCount=126107\n",
      "109 CumCount=3405585 MinX=800000 MinY=1300000 QueryCount=18752 GridCellCount=18752\n",
      "110 CumCount=3436622 MinX=800000 MinY=1200000 QueryCount=31037 GridCellCount=31037\n",
      "111 CumCount=3477912 MinX=1600000 MinY=-1100000 QueryCount=41290 GridCellCount=41290\n",
      "112 CumCount=3516371 MinX=800000 MinY=800000 QueryCount=38459 GridCellCount=38459\n",
      "113 CumCount=3563629 MinX=800000 MinY=500000 QueryCount=47258 GridCellCount=47258\n",
      "114 CumCount=3610329 MinX=800000 MinY=200000 QueryCount=46700 GridCellCount=46700\n",
      "115 CumCount=3660857 MinX=800000 MinY=-200000 QueryCount=50528 GridCellCount=50528\n",
      "116 CumCount=3660889 MinX=1300000 MinY=-600000 QueryCount=32 GridCellCount=32\n",
      "117 CumCount=3680521 MinX=800000 MinY=-500000 QueryCount=19632 GridCellCount=19632\n",
      "118 CumCount=3712691 MinX=800000 MinY=-600000 QueryCount=32170 GridCellCount=32170\n",
      "119 CumCount=3808451 MinX=800000 MinY=-800000 QueryCount=95760 GridCellCount=95760\n",
      "120 CumCount=3818953 MinX=800000 MinY=-900000 QueryCount=10502 GridCellCount=10502\n",
      "121 CumCount=3846723 MinX=800000 MinY=-1100000 QueryCount=27770 GridCellCount=27770\n",
      "122 CumCount=3876330 MinX=800000 MinY=-1300000 QueryCount=29607 GridCellCount=29607\n",
      "123 CumCount=3893128 MinX=800000 MinY=-1400000 QueryCount=16798 GridCellCount=16798\n",
      "124 CumCount=3929015 MinX=700000 MinY=1500000 QueryCount=35887 GridCellCount=35887\n",
      "125 CumCount=3966049 MinX=700000 MinY=1400000 QueryCount=37034 GridCellCount=37034\n",
      "126 CumCount=4003184 MinX=700000 MinY=1300000 QueryCount=37135 GridCellCount=37135\n",
      "127 CumCount=4059279 MinX=700000 MinY=1100000 QueryCount=56095 GridCellCount=56095\n",
      "128 CumCount=4067397 MinX=700000 MinY=1000000 QueryCount=8118 GridCellCount=8118\n",
      "129 CumCount=4107947 MinX=700000 MinY=700000 QueryCount=40550 GridCellCount=40550\n",
      "130 CumCount=4154857 MinX=700000 MinY=500000 QueryCount=46910 GridCellCount=46910\n",
      "131 CumCount=4193575 MinX=700000 MinY=200000 QueryCount=38718 GridCellCount=38718\n",
      "132 CumCount=4200759 MinX=700000 MinY=-100000 QueryCount=7184 GridCellCount=7184\n",
      "133 CumCount=4249122 MinX=700000 MinY=-400000 QueryCount=48363 GridCellCount=48363\n",
      "134 CumCount=4279241 MinX=700000 MinY=-600000 QueryCount=30119 GridCellCount=30119\n",
      "135 CumCount=4313402 MinX=700000 MinY=-800000 QueryCount=34161 GridCellCount=34161\n",
      "136 CumCount=4320535 MinX=700000 MinY=-900000 QueryCount=7133 GridCellCount=7133\n",
      "137 CumCount=4365602 MinX=700000 MinY=-1000000 QueryCount=45067 GridCellCount=45067\n",
      "138 CumCount=4365646 MinX=700000 MinY=-1300000 QueryCount=44 GridCellCount=44\n",
      "139 CumCount=4375232 MinX=600000 MinY=1200000 QueryCount=9586 GridCellCount=9586\n",
      "140 CumCount=4411947 MinX=600000 MinY=1100000 QueryCount=36715 GridCellCount=36715\n",
      "141 CumCount=4414728 MinX=600000 MinY=800000 QueryCount=2781 GridCellCount=2781\n",
      "142 CumCount=4445223 MinX=800000 MinY=300000 QueryCount=30495 GridCellCount=30495\n",
      "143 CumCount=4487843 MinX=600000 MinY=700000 QueryCount=42620 GridCellCount=42620\n",
      "144 CumCount=4558206 MinX=600000 MinY=500000 QueryCount=70363 GridCellCount=70363\n",
      "145 CumCount=4630993 MinX=600000 MinY=300000 QueryCount=72787 GridCellCount=72787\n",
      "146 CumCount=4678872 MinX=600000 MinY=100000 QueryCount=47879 GridCellCount=47879\n",
      "147 CumCount=4739508 MinX=600000 MinY=-100000 QueryCount=60636 GridCellCount=60636\n",
      "148 CumCount=4745213 MinX=600000 MinY=-600000 QueryCount=5705 GridCellCount=5705\n",
      "149 CumCount=4749919 MinX=1100000 MinY=-300000 QueryCount=4706 GridCellCount=4706\n",
      "150 CumCount=4801523 MinX=700000 MinY=-1100000 QueryCount=51604 GridCellCount=51604\n",
      "151 CumCount=4814222 MinX=500000 MinY=1400000 QueryCount=12699 GridCellCount=12699\n",
      "152 CumCount=4818754 MinX=500000 MinY=1200000 QueryCount=4532 GridCellCount=4532\n",
      "153 CumCount=4828739 MinX=500000 MinY=1000000 QueryCount=9985 GridCellCount=9985\n",
      "154 CumCount=4894982 MinX=500000 MinY=900000 QueryCount=66243 GridCellCount=66243\n",
      "155 CumCount=4939603 MinX=500000 MinY=700000 QueryCount=44621 GridCellCount=44621\n",
      "156 CumCount=5004560 MinX=500000 MinY=600000 QueryCount=64957 GridCellCount=64957\n",
      "157 CumCount=5068841 MinX=500000 MinY=400000 QueryCount=64281 GridCellCount=64281\n",
      "158 CumCount=5112665 MinX=500000 MinY=300000 QueryCount=43824 GridCellCount=43824\n",
      "159 CumCount=5156827 MinX=500000 MinY=0 QueryCount=44162 GridCellCount=44162\n",
      "160 CumCount=5213375 MinX=500000 MinY=-200000 QueryCount=56548 GridCellCount=56548\n",
      "161 CumCount=5264316 MinX=500000 MinY=-300000 QueryCount=50941 GridCellCount=50941\n",
      "162 CumCount=5296014 MinX=500000 MinY=-500000 QueryCount=31698 GridCellCount=31698\n",
      "163 CumCount=5365873 MinX=500000 MinY=-700000 QueryCount=69859 GridCellCount=69859\n",
      "164 CumCount=5412608 MinX=400000 MinY=1600000 QueryCount=46735 GridCellCount=46735\n",
      "165 CumCount=5446582 MinX=400000 MinY=1300000 QueryCount=33974 GridCellCount=33974\n",
      "166 CumCount=5486669 MinX=400000 MinY=1200000 QueryCount=40087 GridCellCount=40087\n",
      "167 CumCount=5510767 MinX=400000 MinY=1100000 QueryCount=24098 GridCellCount=24098\n",
      "168 CumCount=5513471 MinX=400000 MinY=900000 QueryCount=2704 GridCellCount=2704\n",
      "169 CumCount=5579348 MinX=400000 MinY=800000 QueryCount=65877 GridCellCount=65877\n",
      "170 CumCount=5628032 MinX=600000 MinY=200000 QueryCount=48684 GridCellCount=48684\n",
      "171 CumCount=5648063 MinX=400000 MinY=700000 QueryCount=20031 GridCellCount=20031\n",
      "172 CumCount=5728150 MinX=400000 MinY=500000 QueryCount=80087 GridCellCount=80087\n",
      "173 CumCount=5801798 MinX=400000 MinY=400000 QueryCount=73648 GridCellCount=73648\n",
      "174 CumCount=5841268 MinX=400000 MinY=300000 QueryCount=39470 GridCellCount=39470\n",
      "175 CumCount=5881788 MinX=600000 MinY=-1000000 QueryCount=40520 GridCellCount=40520\n",
      "176 CumCount=5941659 MinX=400000 MinY=200000 QueryCount=59871 GridCellCount=59871\n",
      "177 CumCount=5999810 MinX=400000 MinY=0 QueryCount=58151 GridCellCount=58151\n",
      "178 CumCount=6071455 MinX=400000 MinY=-200000 QueryCount=71645 GridCellCount=71645\n",
      "179 CumCount=6130289 MinX=400000 MinY=-300000 QueryCount=58834 GridCellCount=58834\n",
      "180 CumCount=6181986 MinX=400000 MinY=-500000 QueryCount=51697 GridCellCount=51697\n",
      "181 CumCount=6244051 MinX=400000 MinY=-600000 QueryCount=62065 GridCellCount=62065\n",
      "182 CumCount=6264908 MinX=400000 MinY=-700000 QueryCount=20857 GridCellCount=20857\n",
      "183 CumCount=6265478 MinX=300000 MinY=1300000 QueryCount=570 GridCellCount=570\n",
      "184 CumCount=6286503 MinX=300000 MinY=1100000 QueryCount=21025 GridCellCount=21025\n",
      "185 CumCount=6322187 MinX=300000 MinY=900000 QueryCount=35684 GridCellCount=35684\n",
      "186 CumCount=6351386 MinX=300000 MinY=800000 QueryCount=29199 GridCellCount=29199\n",
      "187 CumCount=6441431 MinX=300000 MinY=600000 QueryCount=90045 GridCellCount=90045\n",
      "188 CumCount=6465666 MinX=800000 MinY=400000 QueryCount=24235 GridCellCount=24235\n",
      "189 CumCount=6572853 MinX=300000 MinY=500000 QueryCount=107187 GridCellCount=107187\n",
      "190 CumCount=6660610 MinX=300000 MinY=300000 QueryCount=87757 GridCellCount=87757\n",
      "191 CumCount=6724261 MinX=300000 MinY=100000 QueryCount=63651 GridCellCount=63651\n",
      "192 CumCount=6806536 MinX=300000 MinY=0 QueryCount=82275 GridCellCount=82275\n",
      "193 CumCount=6870412 MinX=300000 MinY=-400000 QueryCount=63876 GridCellCount=63876\n",
      "194 CumCount=6874229 MinX=1100000 MinY=-1700000 QueryCount=3817 GridCellCount=3817\n",
      "195 CumCount=6917873 MinX=200000 MinY=1400000 QueryCount=43644 GridCellCount=43644\n",
      "196 CumCount=6940418 MinX=200000 MinY=1300000 QueryCount=22545 GridCellCount=22545\n",
      "197 CumCount=6958945 MinX=200000 MinY=800000 QueryCount=18527 GridCellCount=18527\n",
      "198 CumCount=6961074 MinX=200000 MinY=700000 QueryCount=2129 GridCellCount=2129\n",
      "199 CumCount=7032002 MinX=200000 MinY=600000 QueryCount=70928 GridCellCount=70928\n",
      "200 CumCount=7142449 MinX=200000 MinY=400000 QueryCount=110447 GridCellCount=110447\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "201 CumCount=7198545 MinX=400000 MinY=100000 QueryCount=56096 GridCellCount=56096\n",
      "202 CumCount=7340991 MinX=200000 MinY=300000 QueryCount=142446 GridCellCount=142446\n",
      "203 CumCount=7526501 MinX=200000 MinY=100000 QueryCount=185510 GridCellCount=185510\n",
      "204 CumCount=7617551 MinX=200000 MinY=-300000 QueryCount=91050 GridCellCount=91050\n",
      "205 CumCount=7633883 MinX=100000 MinY=1600000 QueryCount=16332 GridCellCount=16332\n",
      "206 CumCount=7657105 MinX=100000 MinY=1500000 QueryCount=23222 GridCellCount=23222\n",
      "207 CumCount=7725755 MinX=100000 MinY=1300000 QueryCount=68650 GridCellCount=68650\n",
      "208 CumCount=7823426 MinX=100000 MinY=1200000 QueryCount=97671 GridCellCount=97671\n",
      "209 CumCount=7846161 MinX=200000 MinY=900000 QueryCount=22735 GridCellCount=22735\n",
      "210 CumCount=7895427 MinX=100000 MinY=1000000 QueryCount=49266 GridCellCount=49266\n",
      "211 CumCount=7922119 MinX=100000 MinY=900000 QueryCount=26692 GridCellCount=26692\n",
      "212 CumCount=7987501 MinX=600000 MinY=-500000 QueryCount=65382 GridCellCount=65382\n",
      "213 CumCount=8032972 MinX=100000 MinY=800000 QueryCount=45471 GridCellCount=45471\n",
      "214 CumCount=8098353 MinX=100000 MinY=500000 QueryCount=65381 GridCellCount=65381\n",
      "215 CumCount=8133140 MinX=1200000 MinY=700000 QueryCount=34787 GridCellCount=34787\n",
      "216 CumCount=8137990 MinX=100000 MinY=0 QueryCount=4850 GridCellCount=4850\n",
      "217 CumCount=8142260 MinX=100000 MinY=-100000 QueryCount=4270 GridCellCount=4270\n",
      "218 CumCount=8169957 MinX=700000 MinY=-300000 QueryCount=27697 GridCellCount=27697\n",
      "219 CumCount=8183763 MinX=0 MinY=1600000 QueryCount=13806 GridCellCount=13806\n",
      "220 CumCount=8192952 MinX=0 MinY=1500000 QueryCount=9189 GridCellCount=9189\n",
      "221 CumCount=8199784 MinX=0 MinY=1400000 QueryCount=6832 GridCellCount=6832\n",
      "222 CumCount=8200478 MinX=1100000 MinY=500000 QueryCount=694 GridCellCount=694\n",
      "223 CumCount=8244233 MinX=0 MinY=1000000 QueryCount=43755 GridCellCount=43755\n",
      "224 CumCount=8303756 MinX=0 MinY=900000 QueryCount=59523 GridCellCount=59523\n",
      "225 CumCount=8331648 MinX=1100000 MinY=1400000 QueryCount=27892 GridCellCount=27892\n",
      "226 CumCount=8392249 MinX=0 MinY=400000 QueryCount=60601 GridCellCount=60601\n",
      "227 CumCount=8456882 MinX=0 MinY=100000 QueryCount=64633 GridCellCount=64633\n",
      "228 CumCount=8479938 MinX=-100000 MinY=1600000 QueryCount=23056 GridCellCount=23056\n",
      "229 CumCount=8743833 MinX=200000 MinY=-100000 QueryCount=263895 GridCellCount=263895\n",
      "230 CumCount=8773071 MinX=-100000 MinY=1500000 QueryCount=29238 GridCellCount=29238\n",
      "231 CumCount=8794329 MinX=1400000 MinY=-1200000 QueryCount=21258 GridCellCount=21258\n",
      "232 CumCount=8819771 MinX=-100000 MinY=1400000 QueryCount=25442 GridCellCount=25442\n",
      "233 CumCount=8847690 MinX=-100000 MinY=1300000 QueryCount=27919 GridCellCount=27919\n",
      "234 CumCount=8859778 MinX=-100000 MinY=1200000 QueryCount=12088 GridCellCount=12088\n",
      "235 CumCount=8873877 MinX=1300000 MinY=1100000 QueryCount=14099 GridCellCount=14099\n",
      "236 CumCount=8888206 MinX=-100000 MinY=1100000 QueryCount=14329 GridCellCount=14329\n",
      "237 CumCount=8907316 MinX=-100000 MinY=1000000 QueryCount=19110 GridCellCount=19110\n",
      "238 CumCount=8937375 MinX=-100000 MinY=900000 QueryCount=30059 GridCellCount=30059\n",
      "239 CumCount=8959813 MinX=800000 MinY=100000 QueryCount=22438 GridCellCount=22438\n",
      "240 CumCount=9071056 MinX=-100000 MinY=300000 QueryCount=111243 GridCellCount=111243\n",
      "241 CumCount=9102815 MinX=600000 MinY=900000 QueryCount=31759 GridCellCount=31759\n",
      "242 CumCount=9121758 MinX=-200000 MinY=1500000 QueryCount=18943 GridCellCount=18943\n",
      "243 CumCount=9193988 MinX=-200000 MinY=1200000 QueryCount=72230 GridCellCount=72230\n",
      "244 CumCount=9256968 MinX=-200000 MinY=1100000 QueryCount=62980 GridCellCount=62980\n",
      "245 CumCount=9304284 MinX=-200000 MinY=1000000 QueryCount=47316 GridCellCount=47316\n",
      "246 CumCount=9538962 MinX=200000 MinY=0 QueryCount=234678 GridCellCount=234678\n",
      "247 CumCount=9586670 MinX=-200000 MinY=800000 QueryCount=47708 GridCellCount=47708\n",
      "248 CumCount=9652101 MinX=500000 MinY=-100000 QueryCount=65431 GridCellCount=65431\n",
      "249 CumCount=9779683 MinX=-200000 MinY=700000 QueryCount=127582 GridCellCount=127582\n",
      "250 CumCount=9857922 MinX=-200000 MinY=600000 QueryCount=78239 GridCellCount=78239\n",
      "251 CumCount=9929855 MinX=800000 MinY=0 QueryCount=71933 GridCellCount=71933\n",
      "252 CumCount=10233389 MinX=-200000 MinY=200000 QueryCount=303534 GridCellCount=303534\n",
      "253 CumCount=10237445 MinX=-200000 MinY=0 QueryCount=4056 GridCellCount=4056\n",
      "254 CumCount=10242159 MinX=-200000 MinY=-100000 QueryCount=4714 GridCellCount=4714\n",
      "255 CumCount=10682489 MinX=-200000 MinY=-200000 QueryCount=440330 GridCellCount=440330\n",
      "256 CumCount=10709022 MinX=-300000 MinY=1500000 QueryCount=26533 GridCellCount=26533\n",
      "257 CumCount=10716843 MinX=-300000 MinY=1400000 QueryCount=7821 GridCellCount=7821\n",
      "258 CumCount=10716897 MinX=-300000 MinY=1300000 QueryCount=54 GridCellCount=54\n",
      "259 CumCount=10745734 MinX=600000 MinY=-200000 QueryCount=28837 GridCellCount=28837\n",
      "260 CumCount=10776208 MinX=-300000 MinY=1000000 QueryCount=30474 GridCellCount=30474\n",
      "261 CumCount=10830575 MinX=-300000 MinY=900000 QueryCount=54367 GridCellCount=54367\n",
      "262 CumCount=10832263 MinX=-300000 MinY=700000 QueryCount=1688 GridCellCount=1688\n",
      "263 CumCount=10833829 MinX=900000 MinY=800000 QueryCount=1566 GridCellCount=1566\n",
      "264 CumCount=10849882 MinX=600000 MinY=-400000 QueryCount=16053 GridCellCount=16053\n",
      "265 CumCount=10893273 MinX=-300000 MinY=600000 QueryCount=43391 GridCellCount=43391\n",
      "266 CumCount=11361790 MinX=-200000 MinY=100000 QueryCount=468517 GridCellCount=468517\n",
      "267 CumCount=11447445 MinX=-300000 MinY=400000 QueryCount=85655 GridCellCount=85655\n",
      "268 CumCount=11682557 MinX=-300000 MinY=200000 QueryCount=235112 GridCellCount=235112\n",
      "269 CumCount=12056089 MinX=-300000 MinY=100000 QueryCount=373532 GridCellCount=373532\n",
      "270 CumCount=12118617 MinX=1600000 MinY=-1300000 QueryCount=62528 GridCellCount=62528\n",
      "271 CumCount=12711535 MinX=-300000 MinY=0 QueryCount=592918 GridCellCount=592918\n",
      "272 CumCount=12734739 MinX=1500000 MinY=-600000 QueryCount=23204 GridCellCount=23204\n",
      "273 CumCount=12795562 MinX=0 MinY=700000 QueryCount=60823 GridCellCount=60823\n",
      "274 CumCount=13415225 MinX=-300000 MinY=-100000 QueryCount=619663 GridCellCount=619663\n",
      "275 CumCount=13873324 MinX=-300000 MinY=-200000 QueryCount=458099 GridCellCount=458099\n",
      "276 CumCount=14202114 MinX=-400000 MinY=700000 QueryCount=328790 GridCellCount=328790\n",
      "277 CumCount=14327705 MinX=-400000 MinY=500000 QueryCount=125591 GridCellCount=125591\n",
      "278 CumCount=14428267 MinX=-400000 MinY=300000 QueryCount=100562 GridCellCount=100562\n",
      "279 CumCount=14754293 MinX=-400000 MinY=0 QueryCount=326026 GridCellCount=326026\n",
      "280 CumCount=14818155 MinX=500000 MinY=-900000 QueryCount=63862 GridCellCount=63862\n",
      "281 CumCount=14907139 MinX=100000 MinY=1100000 QueryCount=88984 GridCellCount=88984\n",
      "282 CumCount=15125502 MinX=-500000 MinY=200000 QueryCount=218363 GridCellCount=218363\n",
      "283 CumCount=15268050 MinX=-500000 MinY=100000 QueryCount=142548 GridCellCount=142548\n",
      "284 CumCount=15538521 MinX=-500000 MinY=0 QueryCount=270471 GridCellCount=270471\n",
      "285 CumCount=15581448 MinX=1000000 MinY=400000 QueryCount=42927 GridCellCount=42927\n",
      "286 CumCount=15653136 MinX=-600000 MinY=100000 QueryCount=71688 GridCellCount=71688\n",
      "287 CumCount=15749229 MinX=-600000 MinY=0 QueryCount=96093 GridCellCount=96093\n",
      "288 CumCount=15969026 MinX=-600000 MinY=-100000 QueryCount=219797 GridCellCount=219797\n",
      "289 CumCount=16136066 MinX=-700000 MinY=-100000 QueryCount=167040 GridCellCount=167040\n",
      "290 CumCount=16218193 MinX=-700000 MinY=-200000 QueryCount=82127 GridCellCount=82127\n",
      "291 CumCount=16238531 MinX=600000 MinY=600000 QueryCount=20338 GridCellCount=20338\n",
      "292 CumCount=16433051 MinX=100000 MinY=-200000 QueryCount=194520 GridCellCount=194520\n",
      "293 CumCount=16581791 MinX=-700000 MinY=-300000 QueryCount=148740 GridCellCount=148740\n",
      "294 CumCount=16590764 MinX=-700000 MinY=-600000 QueryCount=8973 GridCellCount=8973\n",
      "295 CumCount=16636532 MinX=-800000 MinY=-100000 QueryCount=45768 GridCellCount=45768\n",
      "296 CumCount=16661143 MinX=1500000 MinY=-200000 QueryCount=24611 GridCellCount=24611\n",
      "297 CumCount=16766373 MinX=-800000 MinY=-200000 QueryCount=105230 GridCellCount=105230\n",
      "298 CumCount=16796650 MinX=-800000 MinY=-400000 QueryCount=30277 GridCellCount=30277\n",
      "299 CumCount=16869426 MinX=-800000 MinY=-500000 QueryCount=72776 GridCellCount=72776\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "300 CumCount=16925406 MinX=-800000 MinY=-600000 QueryCount=55980 GridCellCount=55980\n",
      "301 CumCount=16937473 MinX=-800000 MinY=-800000 QueryCount=12067 GridCellCount=12067\n",
      "302 CumCount=16957343 MinX=-900000 MinY=-100000 QueryCount=19870 GridCellCount=19870\n",
      "303 CumCount=16995687 MinX=800000 MinY=-1200000 QueryCount=38344 GridCellCount=38344\n",
      "304 CumCount=17092177 MinX=-900000 MinY=-200000 QueryCount=96490 GridCellCount=96490\n",
      "305 CumCount=17141301 MinX=-900000 MinY=-300000 QueryCount=49124 GridCellCount=49124\n",
      "306 CumCount=17267374 MinX=1100000 MinY=700000 QueryCount=126073 GridCellCount=126073\n",
      "307 CumCount=17305732 MinX=100000 MinY=1400000 QueryCount=38358 GridCellCount=38358\n",
      "308 CumCount=17403953 MinX=-1000000 MinY=0 QueryCount=98221 GridCellCount=98221\n",
      "309 CumCount=17441873 MinX=1100000 MinY=-100000 QueryCount=37920 GridCellCount=37920\n",
      "310 CumCount=17488020 MinX=-1000000 MinY=-100000 QueryCount=46147 GridCellCount=46147\n",
      "311 CumCount=17523891 MinX=-1000000 MinY=-200000 QueryCount=35871 GridCellCount=35871\n",
      "312 CumCount=17556204 MinX=-1000000 MinY=-400000 QueryCount=32313 GridCellCount=32313\n",
      "313 CumCount=17559902 MinX=-1000000 MinY=-700000 QueryCount=3698 GridCellCount=3698\n",
      "314 CumCount=17587509 MinX=-300000 MinY=800000 QueryCount=27607 GridCellCount=27607\n",
      "315 CumCount=17698785 MinX=-1100000 MinY=-100000 QueryCount=111276 GridCellCount=111276\n",
      "316 CumCount=17712344 MinX=-1100000 MinY=-200000 QueryCount=13559 GridCellCount=13559\n",
      "317 CumCount=17755606 MinX=-1100000 MinY=-300000 QueryCount=43262 GridCellCount=43262\n",
      "318 CumCount=17775626 MinX=-1100000 MinY=-400000 QueryCount=20020 GridCellCount=20020\n",
      "319 CumCount=17780295 MinX=-1100000 MinY=-500000 QueryCount=4669 GridCellCount=4669\n",
      "320 CumCount=17799949 MinX=1500000 MinY=-500000 QueryCount=19654 GridCellCount=19654\n",
      "321 CumCount=17860290 MinX=-1100000 MinY=-600000 QueryCount=60341 GridCellCount=60341\n",
      "322 CumCount=17860507 MinX=-1200000 MinY=-200000 QueryCount=217 GridCellCount=217\n",
      "323 CumCount=17903354 MinX=-1200000 MinY=-300000 QueryCount=42847 GridCellCount=42847\n",
      "324 CumCount=17937643 MinX=-1300000 MinY=-300000 QueryCount=34289 GridCellCount=34289\n",
      "325 CumCount=31619458 MinX=0 MinY=200000 QueryCount=13681815 GridCellCount=13681815\n",
      "326 CumCount=49466867 MinX=-100000 MinY=200000 QueryCount=17847409 GridCellCount=17847409\n",
      "327 CumCount=49565316 MinX=-400000 MinY=1200000 QueryCount=98449 GridCellCount=98449\n",
      "328 CumCount=49722657 MinX=-600000 MinY=-200000 QueryCount=157341 GridCellCount=157341\n",
      "329 CumCount=50082899 MinX=-400000 MinY=1100000 QueryCount=360242 GridCellCount=360242\n",
      "330 CumCount=50882649 MinX=-400000 MinY=900000 QueryCount=799750 GridCellCount=799750\n",
      "331 CumCount=51594275 MinX=-400000 MinY=800000 QueryCount=711626 GridCellCount=711626\n",
      "332 CumCount=60022771 MinX=-500000 MinY=600000 QueryCount=8428496 GridCellCount=8428496\n",
      "333 CumCount=60072874 MinX=1300000 MinY=100000 QueryCount=50103 GridCellCount=50103\n",
      "334 CumCount=73607755 MinX=-500000 MinY=500000 QueryCount=13534881 GridCellCount=13534881\n",
      "335 CumCount=78687816 MinX=-600000 MinY=200000 QueryCount=5080061 GridCellCount=5080061\n",
      "336 CumCount=99277030 MinX=-700000 MinY=300000 QueryCount=20589214 GridCellCount=20589214\n",
      "337 CumCount=118988248 MinX=-700000 MinY=200000 QueryCount=19711218 GridCellCount=19711218\n",
      "338 CumCount=118995255 MinX=300000 MinY=1200000 QueryCount=7007 GridCellCount=7007\n",
      "339 CumCount=119092196 MinX=300000 MinY=-500000 QueryCount=96941 GridCellCount=96941\n",
      "340 CumCount=119430481 MinX=-400000 MinY=-100000 QueryCount=338285 GridCellCount=338285\n",
      "341 CumCount=119488040 MinX=-700000 MinY=100000 QueryCount=57559 GridCellCount=57559\n",
      "342 CumCount=119502417 MinX=900000 MinY=-1600000 QueryCount=14377 GridCellCount=14377\n",
      "343 CumCount=135978846 MinX=-800000 MinY=200000 QueryCount=16476429 GridCellCount=16476429\n",
      "344 CumCount=146860575 MinX=-900000 MinY=100000 QueryCount=10881729 GridCellCount=10881729\n",
      "345 CumCount=147077754 MinX=-600000 MinY=-300000 QueryCount=217179 GridCellCount=217179\n",
      "346 CumCount=147129979 MinX=1900000 MinY=-600000 QueryCount=52225 GridCellCount=52225\n",
      "347 CumCount=147203765 MinX=-200000 MinY=500000 QueryCount=73786 GridCellCount=73786\n",
      "348 CumCount=147234920 MinX=800000 MinY=-1600000 QueryCount=31155 GridCellCount=31155\n",
      "349 CumCount=147258951 MinX=1300000 MinY=600000 QueryCount=24031 GridCellCount=24031\n",
      "350 CumCount=147321733 MinX=1700000 MinY=100000 QueryCount=62782 GridCellCount=62782\n",
      "351 CumCount=147410884 MinX=1500000 MinY=1300000 QueryCount=89151 GridCellCount=89151\n",
      "352 CumCount=147496462 MinX=2200000 MinY=700000 QueryCount=85578 GridCellCount=85578\n",
      "353 CumCount=147518412 MinX=1500000 MinY=900000 QueryCount=21950 GridCellCount=21950\n",
      "354 CumCount=147632885 MinX=-1100000 MinY=0 QueryCount=114473 GridCellCount=114473\n",
      "355 CumCount=147694023 MinX=400000 MinY=600000 QueryCount=61138 GridCellCount=61138\n",
      "356 CumCount=147695480 MinX=0 MinY=-800000 QueryCount=1457 GridCellCount=1457\n",
      "357 CumCount=147850124 MinX=-600000 MinY=-400000 QueryCount=154644 GridCellCount=154644\n",
      "358 CumCount=147852934 MinX=2600000 MinY=-100000 QueryCount=2810 GridCellCount=2810\n",
      "359 CumCount=165340886 MinX=200000 MinY=1900000 QueryCount=17487952 GridCellCount=17487952\n",
      "360 CumCount=198480262 MinX=-300000 MinY=-300000 QueryCount=33139376 GridCellCount=33139376\n",
      "361 CumCount=198502780 MinX=1400000 MinY=1300000 QueryCount=22518 GridCellCount=22518\n",
      "362 CumCount=205565629 MinX=200000 MinY=1800000 QueryCount=7062849 GridCellCount=7062849\n",
      "363 CumCount=226595286 MinX=200000 MinY=-500000 QueryCount=21029657 GridCellCount=21029657\n",
      "364 CumCount=238614719 MinX=-1000000 MinY=900000 QueryCount=12019433 GridCellCount=12019433\n",
      "365 CumCount=240298877 MinX=1400000 MinY=400000 QueryCount=1684158 GridCellCount=1684158\n",
      "366 CumCount=242937928 MinX=1900000 MinY=0 QueryCount=2639051 GridCellCount=2639051\n",
      "367 CumCount=242981824 MinX=1500000 MinY=-400000 QueryCount=43896 GridCellCount=43896\n",
      "368 CumCount=253414824 MinX=-2100000 MinY=500000 QueryCount=10433000 GridCellCount=10433000\n",
      "369 CumCount=262924760 MinX=-1700000 MinY=0 QueryCount=9509936 GridCellCount=9509936\n",
      "370 CumCount=263022597 MinX=300000 MinY=200000 QueryCount=97837 GridCellCount=97837\n",
      "371 CumCount=271612278 MinX=100000 MinY=1800000 QueryCount=8589681 GridCellCount=8589681\n",
      "372 CumCount=287059383 MinX=2200000 MinY=400000 QueryCount=15447105 GridCellCount=15447105\n",
      "373 CumCount=331993284 MinX=100000 MinY=-700000 QueryCount=44933901 GridCellCount=44933901\n",
      "374 CumCount=348713703 MinX=100000 MinY=-800000 QueryCount=16720419 GridCellCount=16720419\n",
      "375 CumCount=349442371 MinX=-100000 MinY=2100000 QueryCount=728668 GridCellCount=728668\n",
      "376 CumCount=353862667 MinX=1700000 MinY=200000 QueryCount=4420296 GridCellCount=4420296\n",
      "377 CumCount=354326629 MinX=1800000 MinY=1000000 QueryCount=463962 GridCellCount=463962\n",
      "378 CumCount=354477540 MinX=200000 MinY=200000 QueryCount=150911 GridCellCount=150911\n",
      "379 CumCount=354741871 MinX=2200000 MinY=1100000 QueryCount=264331 GridCellCount=264331\n",
      "380 CumCount=383961734 MinX=-100000 MinY=-300000 QueryCount=29219863 GridCellCount=29219863\n",
      "381 CumCount=383963510 MinX=200000 MinY=1200000 QueryCount=1776 GridCellCount=1776\n",
      "382 CumCount=482394530 MinX=-100000 MinY=-400000 QueryCount=98431020 GridCellCount=98431020\n",
      "383 CumCount=498318350 MinX=-200000 MinY=1800000 QueryCount=15923820 GridCellCount=15923820\n"
     ]
    }
   ],
   "source": [
    "totalcount = 0\n",
    "\n",
    "for rowId, gridCell  in gridCellsDf.iterrows():\n",
    "    count = checkGridCell(gridCell)\n",
    "    totalcount = count + totalcount\n",
    "   \n",
    "    print(\"%d CumCount=%d MinX=%d MinY=%d QueryCount=%d GridCellCount=%d\"%(rowId, totalcount, gridCell['gridCellMinX'], gridCell['gridCellMinY'],count, gridCell['totalPoints']))\n",
    "\n",
    "print( 'Number of rows checked %d' % (totalcount))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Source rows 83171977\n"
     ]
    }
   ],
   "source": [
    "from os import listdir\n",
    "import MalardHelpers as m \n",
    "\n",
    "cdfDir = '/data/puma1/scratch/v2/malard/tempnetcdfs/'\n",
    "\n",
    "files = [f for f in listdir(cdfDir)]\n",
    "sourcecount = 0\n",
    "for f in files:\n",
    "    df = m.getDataFrameFromNetCDF(cdfDir + f)\n",
    "    sourcecount = sourcecount + len(df)\n",
    "\n",
    "print('Source rows %d'%(sourcecount))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#MinX=-600000 MinY=300000 QueryCount=64074 GridCellCount=64077\n",
    "\n",
    "MinX=-600000\n",
    "MaxX = MinX + 100000\n",
    "MinY=300000\n",
    "MaxY=MinY+100000\n",
    "\n",
    "shards = json_normalize(json.loads(query.getShards(parentDs, dataSet, region, MinX, MaxX, MinY, MaxY, minT, maxT )))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "\n",
    "for s in shards['shardName']:\n",
    "    df = m.getDataFrameFromNetCDF(s)\n",
    "    time = datetime.datetime.fromtimestamp(df['time'].min())\n",
    "    startTime = datetime.datetime( time.year, time.month, 1, 0, 0,0  )  \n",
    "    endTime = startTime +relativedelta(months=+1)-timedelta(seconds=1)\n",
    "    \n",
    "    result = asyncquery.executeQuery( parentDs, dataSet, region, MinX, MaxX, MinY, MaxY, startTime, endTime,[],[]   )\n",
    "    dfRes = m.getDataFrameFromNetCDF( result.resultFileName )\n",
    "    \n",
    "    print( 'StartTime %s EndTime %s ShardCount %d QueryCount %d' % (startTime, endTime, len(df['x']), len(dfRes['x'])))\n",
    "    \n",
    "    count = count + len(df['x'])\n",
    "    \n",
    "print(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "maxT = datetime.datetime(2019,12,30,23,59,59)\n",
    "minT = datetime.datetime(2012,1,1,0,0,0)\n",
    "\n",
    "result = asyncquery.executeQuery( parentDs, dataSet, region, MinX, MaxX, MinY, MaxY, minT, maxT,[],[]   )\n",
    "\n",
    "dfRes = m.getDataFrameFromNetCDF( result.resultFileName )\n",
    "\n",
    "print(len(dfRes['x'])  ) \n",
    "\n",
    "dfRes.to_csv(\"first.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "swathDetails = query.getSwathDetails(parentDs, dataSet,region)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
